<template>
  <div class="base-container">
    <t-row :gutter="13">
      <t-col :span="2">
        <div style="display: flex;font-weight: bold;font-size: 30px;padding: 30px">
          创新类别
        </div>
        <t-checkbox-group @change="onChange" style="flex-direction:column;padding: 0 30px"
          v-model="queryParams.innovationCategorys" :options="dict.type.innovation_category"></t-checkbox-group>
      </t-col>
      <t-col :span="10">
        <div style="display: flex;font-weight: bold;justify-content: center;font-size: 30px;padding: 30px">
          待形式审查成果列表
        </div>
        <t-form ref="form" :data="queryParams" label-width="80"
          @submit="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }">
          <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
            <t-col :span="11" class="query-form-inline">
              <t-form-item label="项目名称" name="name">
                <t-input v-model="queryParams.name" clearable placeholder="请输入项目名称" style="width: 240px" />
              </t-form-item>
              <t-form-item label="规避单位" name="managerIdList">
                <t-select multiple style="width: 300px" :keys="{ label: 'deptName', value: 'id' }"
                  :options="deptManagerOptions" clearable filterable v-model="queryParams.managerIdList"></t-select>

              </t-form-item>
            </t-col>
            <t-col :span="1" class="operation-container">
              <t-button theme="primary" type="submit"> 查询</t-button>
              <t-button theme="default" variant="outline" type="reset">重置</t-button>
            </t-col>
          </t-row>
        </t-form>
        <!--        <t-tabs v-if="site_tag === 'jtt'" @change="(val)=>{queryParams.projectCategory=val;getList();}" v-model="queryParams.projectCategory" style="margin-bottom: 10px">-->
        <!--          <t-tab-panel :key="item.id" :value="item.id" v-for="(item) in [{'name': '全部',id: null},...projectCategoryOptions]" :label="item.name" :destroyOnHide="false">-->
        <!--          </t-tab-panel>-->
        <!--        </t-tabs>-->
        <t-table maxHeight="400" row-key="id" :columns="columns" :data="data" :selected-row-keys="selectedRowKeys"
          :loading="dataLoading" @select-change="(val) => { selectedRowKeys = val }">
          <template #name="{ row }">
            <t-link @click="dialog.objId = row.id; dialog.header = row.name; dialog.open_innovation_detail = true"
              hover="color" theme="primary">{{ row.name }}</t-link>
          </template>
          <template #applicationField="{ row }">
            {{ row.applicationField | filterByDict(dict.type.application_field) }}
          </template>
          <template #innovationCategory="{ row }">
            {{ row.innovationCategory | filterByDict(dict.type.innovation_category) }}
          </template>
          <template #innovationStatus="{ row }">
            <t-tag @click="dialog.header = '审批记录'; dialog.objId = row.id; dialog.open_approve_list = true"
              class="cursor-pointer" :theme="handleTheme(row.innovationStatus, dict.type.innovation_status)"
              variant="light">
              {{ row.innovationStatus | filterByDict(dict.type.innovation_status) }}
            </t-tag>
          </template>
          <template #op="{ row }">
            <t-button @click="dialog.header = row.name; dialog.objId = row.id; dialog.open_innovation_detail = true"
              size="small" variant="text" class="t-button-link">
              <t-icon name="info-circle" slot="icon" />
              详情
            </t-button>
          </template>
        </t-table>
        <div style="display: flex;font-weight: bold;justify-content: center;gap:10px;margin-top:30px">
          <t-button :disabled="this.selectedRowKeys.length <= 0" @click="joinList" size="large" theme="default"
            variant="outline">
            <t-icon name="arrow-down" slot="icon"></t-icon>分配</t-button>
          <t-button :disabled="this.selectedRowKeys2.length <= 0" @click="removeList" size="large" theme="default"
            variant="outline"> <t-icon name="arrow-up" slot="icon"></t-icon>移出</t-button>
          <t-button size="large" @click="onSubmit" theme="primary" :loading="applyLoading">{{
            applyLoading ? '提交中...' : '点击提交'
          }}</t-button>
        </div>
        <div style="display: flex;font-weight: bold;justify-content: center;font-size: 30px;padding: 30px">
          已分配成果列表
        </div>
        <t-table maxHeight="400" row-key="id" :columns="columns2" :data="data2" :selected-row-keys="selectedRowKeys2"
          :loading="dataLoading2" @select-change="(val) => { selectedRowKeys2 = val }">
          <template #name="{ row }">
            <t-link @click="dialog.objId = row.id; dialog.header = row.name; dialog.open_innovation_detail = true"
              hover="color" theme="primary">{{ row.name }}</t-link>
          </template>
          <template #applicationField="{ row }">
            {{ row.applicationField | filterByDict(dict.type.application_field) }}
          </template>
          <template #innovationCategory="{ row }">
            {{ row.innovationCategory | filterByDict(dict.type.innovation_category) }}
          </template>
          <template #innovationStatus="{ row }">
            <t-tag @click="dialog.header = '审批记录'; dialog.objId = row.id; dialog.open_approve_list = true"
              class="cursor-pointer" :theme="handleTheme(row.innovationStatus, dict.type.innovation_status)"
              variant="light">
              {{ row.innovationStatus | filterByDict(dict.type.innovation_status) }}
            </t-tag>
          </template>
          <template #[`xsReviewStatus`]="{ row }">
            <t-tag v-if="row.xsReviewStatus"
              @click="dialog.header = row.name; dialog.objId = row.id; dialog.open_approve_list = true"
              class="cursor-pointer" :theme="handleTheme(row.xsReviewStatus, dict.type.xs_review_status)"
              variant="light">
              {{ row.xsReviewStatus | filterByDict(dict.type.xs_review_status) }}
            </t-tag>
            <t-tag @click="dialog.header = row.projectName; dialog.objId = row.id; dialog.open_approve_list = true"
              class="cursor-pointer" v-else theme="warning" variant="light">未评审</t-tag>
          </template>
          <template #op="{ row }">
            <t-button @click="dialog.header = row.name; dialog.objId = row.id; dialog.open_innovation_detail = true"
              size="small" variant="text" class="t-button-link">
              <t-icon name="info-circle" slot="icon" />
              详情
            </t-button>
          </template>
        </t-table>
      </t-col>
    </t-row>

    <innovation-detail-dialog :header="dialog.header" :objId="dialog.objId"
      :visible.sync="dialog.open_innovation_detail" v-if="dialog.open_innovation_detail"></innovation-detail-dialog>

    <approve-list :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_approve_list"
      v-if="dialog.open_approve_list"></approve-list>

  </div>
</template>

<script>
import ApproveList from "./components/approve-list.vue";
import InnovationDetailDialog from "../innovation/components/innovation-detail-dialog.vue";

export default {
  name: "select-project",
  dicts: ["application_field", "innovation_category", "approve_status", "innovation_status", "xs_review_status"],
  components: {
    ApproveList,
    InnovationDetailDialog
  },
  data () {
    return {
      userId: null,
      site_tag: import.meta.env.VITE_SITE_TAG,
      applyLoading: false,
      data2: [],
      dataLoading2: false,
      selectedRowKeys2: [],
      projectCategoryOptions: [],
      dialog: {
        header: "",
        objId: 0,
        open_innovation_detail: false,
        editOpen: true,
        open_approve_list: false
      },
      selectedRowKeys: [],
      queryParams: {
        name: '',
        reviewType: '',
        pageNum: 1,
        pageSize: 10,
        dateRange: [],
        // subjectClassifications: [],
        isAddReview: '0',
        projectCategory: null,
        innovationCategorys: [],
        managerIdList: []
      },
      deptManagerOptions: [],
      data: [],
      dataLoading: false,
      pagination: {
        defaultCurrent: 1,
        defaultPageSize: 10,
        total: 0,
        showPageSize: false,
        showFirstAndLastPageBtn: false,
      },
      columns: [
        {
          colKey: 'row-select',
          type: 'multiple',
          width: 64,
        },
        {
          colKey: "name",
          title: "成果名称",
          align: "center",
          width: 220

        },
        {
          colKey: "applicationProject",
          title: "应用项目",
          align: "center",
        },
        {
          colKey: "applicationField",
          title: "应用领域",
          align: "center",
        },
        {
          colKey: "innovationCategory",
          title: "创新类别",
          align: "center",
        },
        {
          colKey: "leaderName",
          title: "负责人",
          align: "center",
        },
        {
          colKey: "deptName",
          title: "工作单位",
          align: "center",
        },
        {
          colKey: "innovationStatus",
          title: "成果状态",
          align: "center",
          width: 120
        },
        // {
        //   colKey: "approveStatus",
        //   title: "审批状态",
        //   align: "center",
        //   width:120
        // },

        {
          colKey: "createTime",
          title: "申报时间",
          align: "center",
          width: 200
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 120
        },
      ],
      columns2: [
        {
          colKey: 'row-select',
          type: 'multiple',
          width: 64,
          checkProps: ({ row }) => ({ disabled: ['3', '4'].includes(row.xsReviewStatus) }),
        },
        {
          colKey: "name",
          title: "成果名称",
          align: "center",
          width: 220

        },
        {
          colKey: "applicationProject",
          title: "应用项目",
          align: "center",
        },
        {
          colKey: "applicationField",
          title: "应用领域",
          align: "center",
        },
        {
          colKey: "innovationCategory",
          title: "创新类别",
          align: "center",
        },
        {
          colKey: "leaderName",
          title: "负责人",
          align: "center",
        },
        {
          colKey: "deptName",
          title: "工作单位",
          align: "center",
        },
        {
          colKey: "innovationStatus",
          title: "成果状态",
          align: "center",
          width: 120
        },
        // {
        //   colKey: "approveStatus",
        //   title: "审批状态",
        //   align: "center",
        //   width:120
        // },

        {
          colKey: "createTime",
          title: "申报时间",
          align: "center",
          width: 200
        },
        {
          colKey: "xsReviewStatus",
          title: "形式审查状态",
          align: "center",
          width: 120,
          fixed: 'right',
          // sorter: true
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 120,
          fixed: 'right',
        },
      ],
      visible: true,
    }
  },
  async created () {
    const userId = this.$route.params && this.$route.params.userId;
    if (!userId) {
      this.$message.error("用户id丢失返回");
      this.$router.push("/wcx/approve/approver-management");
    }
    this.userId = userId;
    this.getList();
    this.getBindList();
    this.getManagementList();
  },
  methods: {
    getManagementList () {
      this.$api.system.dept.getManagementList().then((res) => {
        this.deptManagerOptions = res.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    onChange (val) {
      this.queryParams.innovationCategorys = val;
      this.getList();
    },
    joinList () {
      const newData = []
      this.data.forEach((val, index) => {
        if (this.selectedRowKeys.includes(val.id)) {
          newData.push({ ...val, rowIndex: index })
        }
      })
      this.data = this.data.filter(val => {
        if (this.selectedRowKeys.includes(val.id)) {
          return false
        }
        return true
      })
      this.data2.push(...newData)
      this.selectedRowKeys = []
    },
    removeList () {
      const newData = []
      this.data2.forEach((val, index) => {
        if (this.selectedRowKeys2.includes(val.id)) {
          newData.push(val)
        }
      })
      this.data2 = this.data2.filter(val => {
        if (this.selectedRowKeys2.includes(val.id)) {
          return false
        }
        return true
      })
      console.log(newData)
      this.data.push(...newData)
      this.selectedRowKeys2 = []
    },
    addReviewProject (projectIds) {
      this.$api.wcx.approve.addReviewProject({
        approverId: this.row.id,
        userName: this.row.nickName,
        projectIds
      }).then((res) => {
        // 初始化
        // this.$message.success("操作成功");
        this.getList();
        this.getBindList();
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    getBindList () {
      this.dataLoading2 = true;
      this.$api.wcx.approve.getApproverReviewByUserId(this.userId).then((res) => {
        this.data2 = res.data;
        this.dataLoading2 = false;
      }).catch((e) => {
        this.dataLoading2 = false
        this.$message.error(e.toString());
      });
    },
    getList () {
      this.dataLoading = true;
      this.queryParams.pageSize = 1000000;
      this.$api.wcx.approve.approvedInnovations(this.queryParams).then(response => {
        // this.pagination.total = response.total;
        this.data = response.rows;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    onSubmit () {
      this.applyLoading = true;
      const ids = this.data2.map(val => val.id)
      this.$api.wcx.approve.addReviewProject({
        approverId: this.userId,
        // userName: this.row.nickName,
        projectIds: ids
      }).then((res) => {
        // 初始化
        this.$message.success("分配成功");
        this.applyLoading = false;
        this.getList();
        this.getBindList();
      }).catch((e) => {
        this.$message.error(e.toString());
        this.applyLoading = false;
      });
    },
  }
}
</script>

<style scoped>
.overlay-options {
  display: inline-block;
  font-size: 20px;
}
</style>
